package org.management.config.pool;


import java.util.Properties;

import javax.servlet.Filter;
import javax.servlet.Servlet;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

import tk.mybatis.spring.mapper.MapperScannerConfigurer;

/**
 * @title: DruidConfig
 * @description: 数据源配置类
 * @author: jiangyan
 * @date:  2019/1/24
 */
@Configuration
public class DruidConfig {

	/**
	 * 数据源管理用户
	 * @return ServletRegistrationBean
	 */
	@Bean
	public ServletRegistrationBean<Servlet> druidServlet() {
		ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(),
				"/druid/*");
		// 登录查看信息的账号密码.
		servletRegistrationBean.addInitParameter("loginUsername", "admin");
		servletRegistrationBean.addInitParameter("loginPassword", "1");
		return servletRegistrationBean;
	}

	/**
	 * 拦截权限配置
	 * @return FilterRegistrationBean
	 */
	@Bean
	public FilterRegistrationBean<Filter> filterRegistrationBean() {
		FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
		filterRegistrationBean.setFilter(new WebStatFilter());
		filterRegistrationBean.addUrlPatterns("/*");
		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		return filterRegistrationBean;
	}

	/**
	 * Mybatis通用Mapper配置UUID
	 * @return MapperScannerConfigurer
	 */
	@Bean
	public MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setBasePackage("org.management.*.dao");
		Properties propertiesMapper = new Properties();
		// 主键UUID回写方法执行顺序,默认AFTER,可选值为(BEFORE|AFTER)
		propertiesMapper.setProperty("ORDER", "BEFORE");
		mapperScannerConfigurer.setProperties(propertiesMapper);
		return mapperScannerConfigurer;
	}

}
